<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>
</body>
<script id="ColorVertexShader" type="x-shader/x-vertex">
    attribute vec4 a_Position;
    uniform float u_PointSize;
    uniform mat4 u_ModelM4;
    uniform mat4 u_PvM4;
    void main(){
        gl_Position = u_PvM4 * u_ModelM4 * a_Position;
        gl_PointSize = u_PointSize;
    }
</script>
<script id="ColorFragmentShader" type="x-shader/x-fragment">
    precision mediump float;
    uniform vec4 u_Color;
    uniform vec2 u_CanvasWH;
    void main(){
        float dist = distance(gl_PointCoord,vec2(0.5,0.5));
        if(dist < 0.5){
            gl_FragColor = vec4(gl_FragCoord.x / u_CanvasWH.x,gl_FragCoord.y / u_CanvasWH.y,1,1);
        }
    }
</script>
<script type="module">
    import { Matrix4, Vector3, Color, OrthographicCamera, PerspectiveCamera, Spherical } from '../js/three.module.js';
    import { CreateProgram, imgPromise } from '../js/Utils.js';
    import lib from '../js/ThreeLib.js';
    import CameraController from '../js/CameraController.js'
    import Poly from '../js/Poly4.js'

    import Geo from '../js/Poly2/Geo.js'
    import Mat from '../js/Poly2/Mat.js'
    import Obj3D from '../js/Poly2/Obj3D.js'
    import Scene from '../js/Poly2/Scene.js'



    //#region gl
    const canvas = document.getElementById("canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight - 4;

    const gl = canvas.getContext("webgl", { antialias: true, });

    // gl.enable(gl.DEPTH_TEST);//开始背景遮挡会频闪
    gl.enable(gl.BLEND);
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
    gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);//去图片描边
    //#endregion


    const scene = new Scene({ gl: gl });

    scene.regProgram('color', {
        program: CreateProgram(gl,
            document.querySelector('#ColorVertexShader').innerText,
            document.querySelector('#ColorFragmentShader').innerText),
        attribs: ['a_Position'],
        uniforms: ['u_PointSize', 'u_ModelM4', 'u_PvM4', 'u_Color', 'u_CanvasWH']
    });

    const pvM4 = new Matrix4();
    const modelM4 = new Matrix4();

    const nodeLength = 0.2;
    const nodeArr = [
        -nodeLength, nodeLength,
        -nodeLength, -nodeLength,
        nodeLength, -nodeLength,
        nodeLength, nodeLength,
    ];

    const elementArr = [
        0, 1, 2,
        0, 2, 3,
    ];

    let radius = 1;

    let obj01 = create(new Matrix4());
    scene.add(obj01);
    let obj02 = create(new Matrix4());
    scene.add(obj02);
    draw();





    let set = 0.0;
    (function ani(time) {

        let a = ((Math.sin(time / 200) + 1) / 2) * 2;
        set += a + 0.2;

        let y = radius * Math.sin(set * Math.PI / 180);
        let x = radius * Math.cos(set * Math.PI / 180);

        obj01.setUniform('u_ModelM4', { value: new Matrix4().setPosition(x, y, 0).elements })
        obj02.setUniform('u_ModelM4', { value: new Matrix4().setPosition(-x, -y, 0).elements })

        draw();
        requestAnimationFrame(ani);
    })(0);


    function create(modelm4) {
        return new Obj3D({
            geo: new Geo({
                data: {
                    'a_Position': { size: 2, array: new Float32Array([0, 0]) },
                },
            }),
            mat: new Mat({
                program: 'color',
                mode: ['POINTS'],
                data: {
                    'u_PvM4': { type: 'uniformMatrix4fv', value: pvM4.elements },
                    'u_ModelM4': { type: 'uniformMatrix4fv', value: modelm4.elements },
                    'u_PointSize': { type: 'uniform1f', value: 100 },
                    'u_Color': { type: 'uniform4fv', value: [1, 1, 1, 1] },
                    'u_CanvasWH': { type: 'uniform2fv', value: [canvas.width, canvas.height] },
                },
            }),
        });
    }

    function draw() {
        scene.setUniform('u_PvM4', { value: pvM4.elements });
        scene.draw();
    }
</script>

</html>